#include "log.h"
#include <QApplication>
#include <log4qt/propertyconfigurator.h>
#include <log4qt/logmanager.h>
#include <QSettings>
#include <QMessageBox>

Log* Log::instance = nullptr;

void Log::initLog()
{
    QString configPath = QApplication::applicationDirPath() + "/QtLog4Qt.conf";
#ifdef Q_OS_MAC
    QString commonOutPath = QApplication::applicationDirPath() + "/Log/application_Log.log";
#else
    QString commonOutPath = "./Log/application_Log.log";
#endif
    QSettings configSet(configPath, QSettings::IniFormat);
    configSet.setIniCodec("UTF-8");
    if(configSet.value("log4j.appender.File.File").toString() != commonOutPath)
        configSet.setValue("log4j.appender.File.File", commonOutPath);
    configSet.sync();
    Log4Qt::PropertyConfigurator::configure(configPath);
    Log4Qt::LogManager::setHandleQtMessages(true);
    declareQML();
}

void Log::logOut(QString logStr, Log::LogLevel level)
{
    Log4Qt::Logger * log = Log4Qt::Logger::rootLogger();
    switch(level) {
    case DEBUG:
        log->warn(logStr);
        break;
    case FATAL:
        log->fatal(logStr);
        break;
    default:
        log->info(logStr);
        break;
    }
}

